<template>
	<view class="purchaseOuter">
		<!-- 地址 -->
		<view class="addressOuter">
			<view class="address" @click="addAddress" v-if="address === null">
				添加收货地址
			</view>
			<view class="zAddress" v-else>
				<view class="zAddress_l">
					<image src="../../static/images/iconImg/myservice/地址.png" mode=""></image>
				</view>
				<view class="zAddress_c">
					<view class="zAddress_c_t">
						<view>收货人：{{address.name}}</view>
						<view>{{address.mobile}}</view>
					</view>
					<view class="zAddress_c_b">
						{{address.area_name}}{{address.address}}
					</view>

				</view>
				<view class="zAddress_r">
					<image src="../../static/images/iconImg/yjt.png" mode=""></image>
				</view>
			</view>
		</view>

		<!-- 商品信息 -->
		<view class="shopInfo" v-for="(item,index) in goodsInfo.data.list" :key="item.id">
			<image class="shopInfo_l" :src="item.products.image_path" mode=""></image>
			<view class="shopInfo_c">
				<view class="shopInfo_c_t">
					{{item.products.name.length>=26?
					item.products.name.slice(0,25)+'...'
					:item.products.name}}
				</view>
				<view class="shopInfo_c_b">
					{{item.products.spes_desc}}
				</view>
			</view>
			<view class="shopInfo_r">
				<view class="shopInfo_r_t">
					￥{{item.products.amount}}
				</view>
				<view class="shopInfo_r_b">
					x {{goodsInfo.data.list[index].nums}}
				</view>
			</view>
		</view>

		<view class="settlementOuter">
			<!-- 优惠券 -->
			<view class="coupon">
				<view>优惠券</view>
				<view>未使用</view>
			</view>

			<!-- 发票 -->
			<view class="invoice">
				<view>发票</view>
				<view class="selectInvoice">
					不开发票
					<image style="width: 20px;height: 16px;" src="../../static/images/iconImg/yjt.png" mode=""></image>
				</view>
			</view>

			<!-- 价格 -->
			<view class="shopPrice">
				<view>商品价格</view>
				<view>￥{{goodsInfo.data.amount}}</view>
			</view>

			<!-- 运费 -->
			<view class="freight">
				<view>运费</view>
				<view>￥0.00</view>
			</view>
		</view>

		<!-- 留言 -->
		<view class="leaveMessage">
			<view>买家留言</view>
			<textarea value="" placeholder="50字以内(选填)" />
		</view>

		<view class="footer">
			<view class="shopNum">
				共<text>{{num}}</text>件商品
			</view>
			<view class="total">
				合计<text>￥{{goodsInfo.data.amount}}</text>
			</view>
			<view class="buyBtn" @click="buyBtn">
				立即支付
			</view>
		</view>
	</view>
</template>

<script>
	import {
		serviceApi
	} from '../../api/paiModule/home.js'
	export default {
		data() {
			return {
				cart_ids: 0,
				num: 0,
				goodsInfo: {},
				address: null,
				areaId: 0,
				uShipId: 0,
				type: 0
			};
		},
		onLoad(options) {
			this.cart_ids = options.ids
		},

		onShow() {
			this.getGoodsInfo({
				area_id: 0,
				coupon_code: "",
				ids: this.cart_ids,
				method: "cart.getlist",
				params: "",
				point: 0,
				receipt_type: 1,
				token: uni.getStorageSync('token'),
				type: 1
			})
			this.getAddress()

		},

		methods: {
			// 获取商品信息
			async getGoodsInfo(data) {
				const {
					data: res
				} = await serviceApi(data)

				console.log(res);

				this.goodsInfo = res
				this.type = res.data.type
				let sum = 0
				res.data.list.forEach(item => {
					sum += item.nums
				})
				this.num = sum
			},

			// 获取用户默认地址
			async getAddress() {
				const {
					data: res
				} = await serviceApi({
					method: "user.getuserdefaultship",
					token: uni.getStorageSync('token')
				})
				this.address = res.data
				this.areaId = res.data.area_id
				this.uShipId = res.data.id
				// console.log(this.areaId, this.uShipId);
			},

			// 添加收货地址
			addAddress() {
				uni.navigateTo({
					url: '/pages/address/address'
				})
			},

			// 支付按钮
			async buyBtn() {
				if (this.address === null) {
					uni.showToast({
						title: '请选择收货地址!',
						icon: 'none',
						duration: 1000
					})
				} else {
					const {
						data: res
					} = await serviceApi({
						area_id: this.areaId,
						cart_ids: "" + this.cart_ids,
						coupon_code: "",
						memo: "",
						method: "order.create",
						point: 0,
						receipt_type: 1,
						source: 2,
						tax_code: "",
						tax_name: "不开发票",
						tax_type: "1",
						token: uni.getStorageSync('token'),
						uship_id: this.uShipId
					})
					uni.navigateTo({
						url: `/pages/pay/pay?ids=${res.data.order_id}&type=${res.data.order_type}`
					})
				}
			},
		},
	}
</script>

<style lang="scss" scoped>
	$H: 93.4vh;

	.purchaseOuter {
		height: $H;
		background-color: #f1f1f1;

		.zAddress {
			display: flex;
			align-items: center;
			padding: 10px;

			.zAddress_l {
				margin-right: 20px;

				image {
					width: 20px;
					height: 26px;
				}
			}

			.zAddress_c {
				font-size: 13px;
				width: 80%;
				margin-right: 16px;

				.zAddress_c_t {
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-bottom: 10px;
				}
			}

			.zAddress_r {
				image {
					width: 14px;
					height: 16px;
				}
			}
		}

		.addressOuter {
			border-bottom: 1px solid #dddddd;
			padding-bottom: 10px;
			background-color: #fff;

			.address {
				width: 28%;
				margin: 0 auto;
				background-color: #000;
				color: #fff;
				text-align: center;
				font-size: 12px;
				padding: 8px 0;
				margin-top: 10px;
			}
		}

		.shopInfo {
			width: 100%;
			display: flex;
			align-items: flex-start;
			padding: 20px 0 10px 10px;
			background-color: #fff;

			image {
				width: 80px;
				height: 80px;
				margin-right: 10px;
			}

			.shopInfo_c {
				width: 48%;
				font-size: 14px;
				margin-right: 26px;

				.shopInfo_c_t {
					width: 100%;
					margin-bottom: 10px;
				}

				.shopInfo_c_b {
					font-size: 12px;
					color: #909090;
				}
			}

			.shopInfo_r {
				font-size: 14px;
				display: flex;
				flex-direction: column;
				align-items: flex-end;

				.shopInfo_r_t {
					margin-bottom: 26px;
				}

				.shopInfo_r_b {
					font-size: 12px;
					color: #909090;
				}
			}
		}

		.settlementOuter {
			background-color: #fff;
			margin-top: 10px;
			padding: 10px;
			font-size: 13px;

			.coupon {
				display: flex;
				align-items: center;
				justify-content: space-between;
				width: 90%;
				border-bottom: 1px solid #ebebeb;
				padding-bottom: 10px;
				margin: 0 auto;
			}

			.invoice {
				width: 90%;
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 10px 0;
				border-bottom: 1px solid #ebebeb;
				margin: 0 auto;

				.selectInvoice {
					display: flex;
					align-items: center;
				}
			}

			.shopPrice {
				width: 90%;
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 10px 0 6px 0;
				margin: 0 auto;

				view {
					&:last-child {
						color: #ff0000;
					}
				}
			}

			.freight {
				width: 90%;
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin: 0 auto;
			}
		}

		.leaveMessage {
			padding: 10px;
			background-color: #fff;
			margin-top: 10px;
			font-size: 14px;

			textarea {
				font-size: 14px;
				padding-top: 10px;
			}
		}

		.footer {
			height: 40px;
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			background-color: #fff;
			display: flex;
			align-items: center;
			font-size: 13px;
			padding: 0 0 0 10px;

			.shopNum {
				color: #8c8c8c;
				margin-right: 40px;
			}

			.total {
				margin-right: 10px;

				text {
					color: #ff5858;
				}
			}

			.buyBtn {
				background-color: #333333;
				color: #fff;
				height: 100%;
				width: 160px;
				line-height: 40px;
				text-align: center;
				position: absolute;
				right: 0;
			}
		}

	}
</style>